package com.ly.mpayment.rest.system;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.auth0.jwt.JWT;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.ly.mpayment.core.util.Config;
import com.ly.mpayment.core.util.JsonUtils;
import com.ly.mpayment.core.util.MapUtil;
import com.ly.mpayment.core.util.ResultMessage;
import com.ly.mpayment.core.util.StringUtils;
import com.ly.mpayment.core.util.UserUtil;
import com.ly.mpayment.persist.entity.User;
import com.ly.mpayment.persist.service.downorderlog.IDownOrderLogService;
import com.ly.mpayment.persist.service.user.IUserService;
import com.ly.mpayment.rest.AbsRest;

import net.sf.json.JSONObject;
@Component
@Path("system/downorderlog/")
public class DownOrderLogRest extends AbsRest {

	@Autowired
	IDownOrderLogService logService;

	@Autowired
	IUserService userService;

	@SuppressWarnings("static-access")
	@POST
	@Path("findDownOrderLog")
	public String findDownOrderLog(String data,@Context HttpServletRequest request){
		String result = this.validationParameter(data, request, userService,"start","pageSize","starttime","useruuid","phoneNum","endtime");
		if(result == null){
			String token = request.getHeader("token");
			if (token != null) {
				DecodedJWT jwt;
				try {
					jwt = JWT.decode(token);
					if (jwt != null) {
						JSONObject userInfo = JsonUtils.converToJson(jwt.getSubject());
						if (userInfo != null) {
							JSONObject json = JSONObject.fromObject(data);
							Map<String, Object> parameters = MapUtil.getMap();
							User adminUser = userService.findByUserid(userInfo.getString(Config.tokenSignAttr));
							if (adminUser != null && UserUtil.checkRoleDown(adminUser)) {
								parameters.put(":useruuid", adminUser.getUseruuid());
							}

							if(!StringUtils.isBlank(json.getString("useruuid"))){
								parameters.put(":useruuid", json.getString("useruuid"));
							}

							if(!StringUtils.isBlank(json.getString("starttime"))){
								parameters.put(":starttime", json.getString("starttime"));
							}

							if(!StringUtils.isBlank(json.getString("endtime"))){
								parameters.put(":endtime", json.getString("endtime"));
							}

							if(!StringUtils.isBlank(json.getString("phoneNum"))){
								parameters.put(":phoneNum", json.getString("phoneNum"));
							}

							return ResultMessage.getSucceed( JsonUtils.excludes(this.logService.findPageDownorderlog(parameters, json.getInt("start"), json.getInt("pageSize")),
									new String[]{"downRechargeGame","","useruuid","errorCodesForUseruuid","expire",
											"wechatid","lastlogintime","usertype","token","userstatus","password","phonenum","downShopProduct",
											"orderMatchDetails","upOrders","downOrderLogs","downOrderRefunds","flowInformation","userFlow","downShopProduct",
											"operators","downreqid","statusremark","province","downendtime","rechargeamount","downrechargenum","downnotificationaddress",
											"downendtimesec","downreqnum","busCode","callStatus","discount","callNum","discountAmount","","","","","","","","",""}));
						}
					}
				}catch(Exception e){
					e.printStackTrace();
				}
			}
		}
		return result;
	}
}
